FROM php:7.2-cli

LABEL maintainer="chenwenlong <chenwenlong@epweike.com>"

# Version
ARG PHPREDIS_VERSION=4.0.0
ARG HIREDIS_VERSION=0.13.3
ARG SWOOLE_VERSION=4.2.4
ARG PORT=9501

COPY sources.list /etc/apt/sources.list

# Timezone
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo 'Asia/Shanghai' > /etc/timezone

# Libs
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        curl \
        wget \
        git \
        zip \
        libz-dev \
        libssl-dev \
        libnghttp2-dev \
        libpcre3-dev \
    && apt-get clean \
    && apt-get autoremove

# PDO Bcmath extension
RUN docker-php-ext-install pdo_mysql bcmath

# pcntl extension
RUN docker-php-ext-install pcntl

# Redis extension
RUN wget http://pecl.php.net/get/redis-${PHPREDIS_VERSION}.tgz -O /tmp/redis.tar.tgz \
    && pecl install /tmp/redis.tar.tgz \
    && rm -rf /tmp/redis.tar.tgz \
    && docker-php-ext-enable redis

# Hiredis
RUN wget https://github.com/redis/hiredis/archive/v${HIREDIS_VERSION}.tar.gz -O hiredis.tar.gz \
    && mkdir -p hiredis \
    && tar -xf hiredis.tar.gz -C hiredis --strip-components=1 \
    && rm hiredis.tar.gz \
    && ( \
        cd hiredis \
        && make -j$(nproc) \
        && make install \
        && ldconfig \
    ) \
    && rm -r hiredis

# Swoole extension
RUN wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz \
    && mkdir -p swoole \
    && tar -xf swoole.tar.gz -C swoole --strip-components=1 \
    && rm swoole.tar.gz \
    && ( \
        cd swoole \
        && phpize \
        && ./configure --enable-async-redis --enable-mysqlnd --enable-openssl --enable-http2 \
        && make -j$(nproc) \
        && make install \
    ) \
    && rm -r swoole \
    && docker-php-ext-enable swoole

# Composer
ARG INSTALL_COMPOSER=false
ENV COMPOSER_ALLOW_SUPERUSER 1

RUN if [ ${INSTALL_COMPOSER} = true ]; then \
    curl -sS https://getcomposer.org/installer | php \
    && mv composer.phar /usr/local/bin/composer \
    && composer self-update --clean-backups \
    && composer config -g repo.packagist composer https://packagist.laravel-china.org \
;fi

###########################################################################
# PHP Memcached:
###########################################################################

ARG INSTALL_MEMCACHED=true

RUN if [ ${INSTALL_MEMCACHED} = true ]; then \
    apt-get update \
    && apt-get install -y libmemcached-dev --no-install-recommends \
    # Install the php memcached extension
    && curl -L -o /tmp/memcached.tar.gz "https://github.com/php-memcached-dev/php-memcached/archive/php7.tar.gz" \
    && mkdir -p memcached \
    && tar -C memcached -zxvf /tmp/memcached.tar.gz --strip 1 \
    && ( \
        cd memcached \
        && phpize \
        && ./configure \
        && make -j$(nproc) \
        && make install \
    ) \
    && rm -r memcached \
    && rm /tmp/memcached.tar.gz \
    && docker-php-ext-enable memcached \
;fi

###########################################################################
# solr:
###########################################################################

ARG INSTALL_SOLR=true

RUN if [ ${INSTALL_SOLR} = true ]; then \
      apt-get update && apt-get install -y libcurl4-openssl-dev libxml2-dev git \
      && git clone https://github.com/php/pecl-search_engine-solr.git --depth=1 \
      && cd pecl-search_engine-solr \
      && /usr/local/bin/phpize && ./configure && make && make install \
      && cd .. && rm -rf pecl-search_engine-solr \
      && apt-get remove --purge -y git \
      && rm -r /var/lib/apt/lists/* \
      && docker-php-ext-enable solr \
;fi

RUN docker-php-ext-install sockets
RUN docker-php-ext-install sysvmsg

ARG INSTALL_VIM=false
RUN if [ ${INSTALL_VIM} = true ]; then \
      apt-get update && apt-get install -y vim \
#      && apt-file update \
#      && apt-get install vim \
;fi

WORKDIR /var/www

EXPOSE ${PORT}

CMD ["php bin/swoft start"]